//
//  MoivedeetaViewController.m
//  豆瓣zlw
//
//  Created by lanou3g on 16/1/10.
//  Copyright © 2016年 zlw. All rights reserved.
//

#import "MoivedeetaViewController.h"
#import <UIImageView+WebCache.h>
#import "MovieModel.h"
#import "MovieDetaModel.h"
#import "LoginViewController.h"
#import "Moive_Data.h"

@interface MoivedeetaViewController ()
{
    NSMutableArray *arrAllData;
}

@end

@implementation MoivedeetaViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=self.getmodel.movieName;

    [self datas];

    
        [self.HUD hide:YES];
 
}
-(void)datas
{
    NSString *fieldPath=@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=";
 NSString *newFeidPath =  [fieldPath stringByAppendingString:self.getmodel.MovieId];
    
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:newFeidPath]];
    NSURLSession *session=[NSURLSession sharedSession];
    NSURLSessionTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data)
        {
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            arrAllData=[NSMutableArray array];
           
                           MovieDetaModel *modelDeta=[[MovieDetaModel alloc] init];
                [modelDeta setValuesForKeysWithDictionary:dict[@"result"]];
            self.getRating.text=modelDeta.rating;
            self.getActors.text=modelDeta.actors;
            self.getRating_count.text=[NSString stringWithFormat:@"%@",modelDeta.rating_count];
            self.getRelease_date.text=modelDeta.release_date;
            self.getRuntime.text=modelDeta.runtime;
            self.getCountry.text=modelDeta.country;
            self.getgenres.text=modelDeta.genres;
            self.getPlot_simple.text=modelDeta.plot_simple;
            [self.getPoster sd_setImageWithURL:[NSURL URLWithString:modelDeta.poster]];
         
        }
           }];
    [task resume];
}

- (IBAction)MovieDe__Back:(id)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (IBAction)MovieDe__Share:(id)sender {
    
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    if ([defaults boolForKey:@"User"])
    {
     //打开数据库
        [[Moive_Data shareMoiveData] openMoiveData];
     //存放数据
        MovieDetaModel *movieModel=[[MovieDetaModel alloc] init];
        movieModel.plot_simple=self.getPlot_simple.text;
        movieModel.tile=self.getmodel.movieName;
        movieModel.genres=self.getgenres.text;
        movieModel.country=self.getCountry.text;
        movieModel.runtime=self.getRuntime.text;

        
        movieModel.poster=self.getmodel.pic_url;
        
        movieModel.rating=self.getRating.text;
        
     movieModel.rating_count=(NSNumber *)self.getRating_count.text;
        
        movieModel.release_date=self.getRelease_date.text;
        movieModel.actors=self.getActors.text;
        [[Moive_Data shareMoiveData] addMoiveData:movieModel];
     
     //关闭数据库
        [[Moive_Data shareMoiveData] closeMoiveData];
        
        
        //显示UIAlertView提示用户
        UIAlertView * alertView = [self showAlertViewWithTitle:@"提示" message:@"收藏成功" cancelButtonTitle:nil otherButtonTitle:nil];
        
        //0.3秒后消失
        [self performSelector:@selector(removeAlertView:) withObject:alertView afterDelay:1];
        

        
        
    }else
    {
        LoginViewController *VC=[[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"MinLog" ];
        [self.navigationController pushViewController:VC animated:YES];
        
    }
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
